<?php
require_once '../config/config.php';
require_once '../config/connexion_db.php';

if (isset($_POST['envoyer'])&& isset($_FILES['fichier'])){
 $copie_fichier =   move_uploaded_file($_FILES['fichier']['tmp_name'], "../img/".$_FILES['fichier']['name']);
 $photo = "/img/".$_FILES['fichier']['name'];

     if(isset($_POST['nom']))      $nom=$_POST['nom'];
        else      $nom="";
     if(isset($_POST['prenom']))      $prenom=$_POST['prenom'];
        else      $prenom="";
     if(isset($_POST['liste_entreprise']))      $id_entreprise=$_POST['liste_entreprise'];
        else      $id_entreprise="";
     if(isset($_POST['telephone']))      $telephone=$_POST['telephone'];
        else      $telephone="";
     if(isset($_POST['email']))      $email=$_POST['email'];
        else      $email="";
     if(isset($_POST['observation']))      $observation=$_POST['observation'];
        else      $observation="";
      $sql = "INSERT INTO carnet_adresses.contacts(id, id_entreprise, nom, prenom, telephone, email, photo, observations) VALUES (NULL, $id_entreprise, '$nom', '$prenom', '$telephone', '$email', '$photo', '$observation')";
      $envoi_requete = mysqli_query($connexion_serveur, $sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
     } ?>

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title>AJOUTER D'UN CONTACT</title>
        <link href="../bootstrap/css/bootstrap.css" type="text/css" rel="stylesheet">
        <link href="../css/style.css" type="text/css" rel="stylesheet">
    </head>
    <body>
        <?php 
        require_once "../config/css_js_bootstrap/css_js_bootstrap.php"; 
        require_once '../config/menus/menu_principal.php';
        ?>
        <div id="fiche">
        <br><div id="div2">
        <a href="liste_contact.php">Retour à la liste des contacts</a>
        <br></div>
        <br><div id="div2">
            <h3>Ajouter un contact</h3>
        <form name="ajouter_contact" id="ajouter_contact" method="post" action="" enctype="multipart/form-data">
            <table>
                <tr>
                    <td class="col1">Nom:</td>
                    <td class="col2"><input type="text" name="nom" id="nom" maxlength="25" size="25"></td>
                </tr>
                <tr>
                    <td class="col1">Prenom:</td>
                    <td class="col2"><input type="text" name="prenom" id="prenom" maxlength="25" size="25"></td>
                </tr>
                <tr>
                    <td class="col1">Entreprise</td>
                    <td class="col2"><select name="liste_entreprise"> 
                        <?php 
                        //REQUETE SQL
                        $sql_ent = "SELECT * FROM entreprises"; //, contacts WHERE entreprises.id = contacts.id_entreprise";
                        $req_ent = mysqli_query($connexion_serveur, $sql_ent);
                        while ($data_ent = mysqli_fetch_assoc($req_ent)){
                        $nbre= mysqli_num_rows($req_ent);
                            ?>
                        <option value="<?php echo $data_ent['id']; ?>"><?php echo $data_ent['raison_sociale']; ?> </option> 
                            <?php
                        }?>
                        </select></td>
                </tr>
                <tr>
                    <td class="col1">Telephone:</td>
                    <td class="col2"><input type="text" name="telephone" id="telephone" maxlength="13" size="13"></td>
                </tr>    
                <tr>
                    <td class="col1">Email:</td>
                    <td class="col2"><input type="text" name="email" id="email" maxlength="30" size="40"></td>
                </tr> 
                <tr>
                    <td class="col1">Photo:</td>
                    <td class="col2"><p><input type="file" name="fichier"></p>
                    </td>
                </tr> 
                <tr>
                    <td class="col1">Observation:</td>
                    <td class="col2"><textarea name="observation" id="observation" cols="60" rows="5" placeholder="Observation..."></textarea></td>
                </tr> 
                    <td class="col1"> </td>
                    <td class="col2"><input type="submit" name="envoyer" value="Envoyer" /></td>
                </tr>
            </table>   
            
        </form>
        </div>
        </div>
    </body>
</html>
